/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.beans.Consulta;
import model.beans.Paciente;
import model.beans.TipoConsulta;
import model.persistence.GerenciadorEntidades;

/**
 *
 * @author kelyane
 */
@WebServlet(name = "ConsultaServlet", urlPatterns = {"/ConsultaServlet"})
public class ConsultaServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        
        String page = request.getParameter("page");
        
        try {
            
            
            
            switch(page){
                case  "listar" : {
                    listConsulta(request,response);
                    break;
                }
                case "insert" :{
                    insertConsulta(request,response);
                    break;
                }
                case "addConsulta" :{
                    renderAddConsulta(request,response);
                    break;
                }
                case "excluir" :{                    
                    excluirConsulta(request,response);
                    break;
                }
                case "renderEditar" : {
                    renderConsulta(request,response);
                    break;
                }
                case "editar" :{
                    editar(request,response);
                    break;
                }
            }
            
            
            
        } finally {            
            out.close();
        }
    }
    
    public void insertConsulta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String idPaciente = request.getParameter("buscaPaciente");
        String medico = request.getParameter("buscaMedico");
        String dia = request.getParameter("diaconsulta");
        String hora = request.getParameter("horaConsulta");
        
        Paciente paciente = GerenciadorPaciente.buscarPaciente(Long.valueOf(idPaciente));                
        Consulta consulta = new Consulta(paciente, medico, new Date(dia), hora, TipoConsulta.normal);        
        GerenciadorConsulta.cadastrarConsulta(consulta);  
        
        RequestDispatcher view = request.getRequestDispatcher("ConsultaServlet?page=listar");        
        view.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void listConsulta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = GerenciadorEntidades.getEntityManager();
        Query query = em.createNamedQuery("findAllConsulta");

        List<Consulta> consultasamedQuery = query.getResultList();
        RequestDispatcher view = request.getRequestDispatcher("list-consulta.jsp");
        request.setAttribute("consultas", consultasamedQuery);
        view.forward(request, response);
    }

    private void renderAddConsulta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = GerenciadorEntidades.getEntityManager();
        Query query = em.createNamedQuery("findAllPaciente");

        List<Paciente> pacientesNamedQuery = query.getResultList();
        RequestDispatcher view = request.getRequestDispatcher("add-up-consulta.jsp");
        request.setAttribute("pacientes", pacientesNamedQuery);
        view.forward(request, response);
    }

    private void excluirConsulta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idconsulta = request.getParameter("id");
        
        Consulta consulta = GerenciadorConsulta.buscarConsulta(Long.valueOf(idconsulta));
        GerenciadorConsulta.removeConsulta(consulta);  
        
        RequestDispatcher view = request.getRequestDispatcher("ConsultaServlet?page=listar");        
        view.forward(request, response);
    }

    private void editar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        String medico = request.getParameter("buscaMedico");
        String dia = request.getParameter("diaconsulta");
        String hora = request.getParameter("horaConsulta");
               
        String idconsulta = request.getParameter("id");
        
        Consulta consulta = GerenciadorConsulta.buscarConsulta(Long.valueOf(idconsulta));
        
        consulta.setMedico(medico);
        consulta.setHora(hora);
        consulta.setHorario(new Date(dia));
        
        GerenciadorConsulta.alterarConsulta(consulta);
        
        RequestDispatcher view = request.getRequestDispatcher("ConsultaServlet?page=listar");        
        view.forward(request, response);
    }

    private void renderConsulta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idconsulta = request.getParameter("id");
        Consulta consulta = GerenciadorConsulta.buscarConsulta(Long.valueOf(idconsulta));
        
        RequestDispatcher view = request.getRequestDispatcher("edit-consulta.jsp");
        request.setAttribute("consulta", consulta);
    
        view.forward(request, response);
    }
    
    
}
